package cn.exrick.service.impl;

import cn.exrick.common.exception.XmallException;
import cn.exrick.mapper.TbFeedbackCatMapper;
import cn.exrick.pojo.TbFeedbackCat;
import cn.exrick.pojo.TbFeedbackCatExample;
import cn.exrick.pojo.common.DataTablesResult;
import cn.exrick.service.FeedbackCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class FeedbackCatServiceImpl implements FeedbackCatService{

    @Autowired
    private TbFeedbackCatMapper tbFeedbackCatMapper;

    @Override
    public int addFeedbackCat(TbFeedbackCat tbFeedbackCat) {
        if(getByName(tbFeedbackCat.getName())!=null){
            throw new XmallException("该分类名已存在");
        }
        tbFeedbackCat.setCreated(new Date());
        tbFeedbackCat.setUpdated(new Date());
        if(tbFeedbackCatMapper.insert(tbFeedbackCat)!=1){
            throw new XmallException("添加反馈分类失败");
        }
        return 1;
    }

    @Override
    public int updateFeedbackCat(TbFeedbackCat tbFeedbackCat) {
        if(!checkNameForEdit(tbFeedbackCat.getId(), tbFeedbackCat.getName())){
            throw new XmallException("该反馈分类名已存在");
        }
        tbFeedbackCat.setUpdated(new Date());
        if(tbFeedbackCatMapper.updateByPrimaryKeySelective(tbFeedbackCat)!=1){
            throw new XmallException("更新反馈分类失败");
        }
        return 1;
    }

    @Override
    public int deleteFeedbackCat(int id) {
        int count = tbFeedbackCatMapper.selectUsedCount(id);
        if(count != 0){
            return 0;
        }
        if(tbFeedbackCatMapper.deleteByPrimaryKey(id)!=1){
            throw new XmallException("删除反馈分类失败");
        }
        return 1;
    }

    @Override
    public List<TbFeedbackCat> getAll() {
        TbFeedbackCatExample example = new TbFeedbackCatExample();
        List<TbFeedbackCat> list = tbFeedbackCatMapper.selectByExample(example);
        if(list == null){
            throw new XmallException("获取反馈分类列表失败");
        }
        return list;
    }

    @Override
    public DataTablesResult getFeedbackCatList() {
        DataTablesResult result = new DataTablesResult();
        result.setData(getAll());
        return result;
    }

    @Override
    public int countFeedbackCat() {
        TbFeedbackCatExample example= new TbFeedbackCatExample();
        return tbFeedbackCatMapper.countByExample(example);
    }

    @Override
    public TbFeedbackCat getByName(String name) {
        TbFeedbackCatExample example = new TbFeedbackCatExample();
        TbFeedbackCatExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name);
        List<TbFeedbackCat> list=new ArrayList<>();
        try {
            list = tbFeedbackCatMapper.selectByExample(example);
        }catch (Exception e){
            throw new XmallException("通过反馈分类名获取分类失败");
        }
        if(!list.isEmpty()){
            return list.get(0);
        }
        return null;
    }

    @Override
    public boolean checkNameForEdit(int id, String name){
        TbFeedbackCat tbFeedbackCat = tbFeedbackCatMapper.selectByPrimaryKey(id);
        TbFeedbackCat newFeedbackCat = null;
        if(tbFeedbackCat == null){
            throw new XmallException("通过ID获取反馈分类失败");
        }
        if(!tbFeedbackCat.getName().equals(name)){
            newFeedbackCat = getByName(name);
        }
        return newFeedbackCat == null;
    }

}
